#include "EthData_Check.h"
#include "GlobalData.h"

extern void usart1Printf(char *fmt,...);

SickMoni  Moni_flag;
BufferQueue			Eth_RxQueue;
uint8_t StartFlag[15] = {0x73,0x52,0x41,0x20,0x4C,
												 0x4D,0x44,0x73,0x63,0x61,
												 0x6E,0x64,0x61,0x74,0x61};
uint8_t ValidFlag[14] = {0x44,0x49,0x53,0x54,0x31,
												 0x20,0x33,0x46,0x38,0x30,
												 0x30,0x30,0x30,0x30};
uint8_t SegFlag = 0x20;

void Eth_Queue_Insert(BufferQueue *Queue, uint8_t *Data, uint16_t len)
{
	BufferInfo *Buffer;
	uint8_t pos;
	uint16_t i;
	pos = Queue->head;
	Buffer = &(Queue->Queue[pos]);
	Buffer->size = len;
	for(i=0; i<len; i++){
		Buffer->Pdata[i] = Data[i];
	}
	Buffer->Is_Set = 1;
	if(Queue->size<(Eth_Queue_Max)){
		Queue->size++;
	}
	if(Queue->head<(Eth_Queue_Max-1)){
		Queue->head++;
	}else{
		Queue->head = 0;
	}
}

void Eth_Queue_Export(BufferQueue *Queue, uint8_t *Data, uint16_t len)
{
	BufferInfo *Buffer;
	uint8_t pos;
	uint16_t i;
	pos = Queue->head;
	Buffer = &(Queue->Queue[pos]);
	Buffer->size = len;
	for(i=0; i<len; i++){
		Buffer->Pdata[i] = Data[i];
	}
	Buffer->Is_Set = 1;
	if(Queue->size<(Eth_Queue_Max)){
		Queue->size++;
	}
	if(Queue->head<(Eth_Queue_Max-1)){
		Queue->head++;
	}else{
		Queue->head = 0;
	}
}

uint8_t Eth_Sick_StartCheck(uint8_t data)
{
	static uint8_t index = 0;
	if(index<15){
		if(data == StartFlag[index]){
			index++;
		}else{
			index = 0;
		}
		return 0;
	}else{
		index = 0;
		return 1;
	}
}
uint8_t Eth_Sick_ValidCheck(uint8_t data)
{
	static uint8_t index = 0;
	if(index<14){
		if(data == ValidFlag[index]){
			index++;
		}else{
			index = 0;
		}
		return 0;
	}else{
		index = 0;
		return 1;
	}	
}
static uint8_t HexToChar(uint8_t bChar)  
{  
    if((bChar>=0x30)&&(bChar<=0x39))  
    {  
        bChar -= 0x30;  
    }  
    else if((bChar>=0x41)&&(bChar<=0x46)) // Capital  
    {  
        bChar -= 0x37;  
    }  
    else if((bChar>=0x61)&&(bChar<=0x66)) //littlecase  
    {  
        bChar -= 0x57;  
    }  
    else   
    {  
        bChar = 0xff;  
    }  
    return bChar;  
} 
static void SickQueue_Insert(SickMoni* Moni_T)
{
	EthInfo* Pinfo;
	uint16_t len;
	uint8_t pos;
	uint16_t i;
	pos = EthQueue.head;
	Pinfo = &(EthQueue.queue[pos]);
	Pinfo->startangle = Moni_T->startangle;
	Pinfo->anglestep = Moni_T->anglestep;
	len = Moni_T->data[0];
	if(len>Max_DataNum){
	  usart1Printf("Error,len->%d  \n", len);
		return;
	}
	for(i=0; i<len+1; i++){
		Pinfo->polardata[i] = Moni_T->data[i];
		Moni_T->data[i] = 0;	
	}	
	if(EthQueue.size<EthQueue_Max){
		EthQueue.size++;		
	}
	if(EthQueue.head<(EthQueue_Max-1)){
		EthQueue.head++;		
	}else{
		EthQueue.head = 0;		
	}
}

//uint16_t u16_Global_temp = 100;
static void Sick_Info_Get(uint8_t data)
{
	static uint16_t index = 0;
	static uint16_t len = 0;
	static uint16_t size = 0;
	int16_t* Pdata;
	uint8_t  Hex_T = 0;
	Pdata= Moni_flag.data;
	if(0==index){
		Moni_flag.startangle = 0;
		Moni_flag.anglestep = 0;
		Moni_flag.len = 0;
		size = 0;
	}
	if(SegFlag==data){
		index++;
		if(4==index){
			if(Moni_flag.len>(Max_DataNum-1)){
			  //error
				Moni_flag.complete = 1;
				Moni_flag.start = 0;
				Moni_flag.valid = 0;
				size = 0;
				len = 0;
				index = 0;
				return;
			}
			size = 0;
			Pdata[size] = Moni_flag.len;
			//usart1Printf("len->%d  \n", Moni_flag.len);
		}
		if(index>=4){
			size++;
		}
//		if(size>=u16_Global_temp){
//			Hex_T = 0;
//			size++;
//			size--;
//		}
	}else{
		Hex_T = HexToChar(data)&0x0F;
		if(index==1){
			//start angle get			
			Moni_flag.startangle = (Moni_flag.startangle<<4)+Hex_T;
		}
		if(2==index){
			//step get
			Moni_flag.anglestep = (Moni_flag.anglestep<<4)+Hex_T;
		}
		if(3==index){
			//data size get
			Moni_flag.len = (Moni_flag.len<<4)+Hex_T;
		}else{
			//data get			
			if(index<(Moni_flag.len+3+1)){
				Pdata[size] = (Pdata[size]<<4) + Hex_T;
			}else{
				//search cmplete
				SickQueue_Insert(&Moni_flag);
				Moni_flag.complete = 1;
				Moni_flag.start = 0;
				Moni_flag.valid = 0;
				size = 0;
				len = 0;
				index = 0;
			}			
		}
	}
}
//uint16_t u16_global_j = 0;
void Eth_Data_process(void)
{
	uint8_t i, result;
	uint8_t *Pdata;
	uint16_t j, len ;
	for(i=0; i<3; i++)
	{
		if(1==Eth_Buffer[i].set)
		{
			Eth_Buffer[i].set = 0;
			//data check
			//1.flag show check
			len = Eth_Buffer[i].size;
			Pdata = Eth_Buffer[i].buffer;
			for(j=0; j<len; j++)
			{
				//u16_global_j = j;
				if(0==Moni_flag.start)
				{
					//start check:sRA LMDscandata
					result = Eth_Sick_StartCheck(Pdata[j]);
					if(1== result){
						Moni_flag.start = 1;
					}
				}
				else
				{
					//valid flag check: 3F800000 00000000
					if(0==Moni_flag.valid){
						result = Eth_Sick_ValidCheck(Pdata[j]);
						if(1==result)
						{
							Moni_flag.valid = 1;
						}else{
							if(2==result){
								//invalid data checked
								Moni_flag.start = 0;
							}
						}						
					}else{
						//real data get
						Sick_Info_Get(Pdata[j]);
					}
				}
			}
		}
	}
}

void Eth_Data_process_ontime(Eth_RxBuffer* PBuffer)
{
	uint8_t result;
	uint8_t *Pdata;
	uint16_t j, len ;
	if(1==PBuffer->set)
	{
		PBuffer->set = 0;
		//data check
		//1.flag show check
		len = PBuffer->size;
		Pdata = PBuffer->buffer;
		for(j=0; j<len; j++)
		{
			//u16_global_j = j;
			if(0==Moni_flag.start)
			{
				//start check:sRA LMDscandata
				result = Eth_Sick_StartCheck(Pdata[j]);
				if(1== result){
					Moni_flag.start = 1;
				}
			}
			else
			{
				//valid flag check: 3F800000 00000000
				if(0==Moni_flag.valid){
					result = Eth_Sick_ValidCheck(Pdata[j]);
					if(1==result)
					{
						Moni_flag.valid = 1;
					}else{
						if(2==result){
							//invalid data checked
							Moni_flag.start = 0;
						}
					}						
				}else{
					//real data get
					Sick_Info_Get(Pdata[j]);
				}
			}
		}
	}
}

